﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enterprise.Utility
{
    public static class RequestExtend {

        /// <summary>
        /// 使用当前请求路径相对于站点根的目录层级,如:
        /// "/a/t.aspx",将得到"../"
        /// "/a/b/test.aspx",将得到"../../"
        /// </summary>
        /// <param name="Request"></param>
        /// <returns></returns>
        public static string GetRelativeRootPath(this System.Web.HttpRequest Request) {
            string startPath = "";
            string currPath = Request.Path;
            string rootPath = Request.ApplicationPath;
            if (currPath.IndexOf(rootPath) > -1) {
                System.Text.StringBuilder appPath = new System.Text.StringBuilder();
                int count = currPath.Substring(rootPath.Length - 1).Split('/').Length;
                for (int i = 2; i < count; i++) appPath.Append("../");
                startPath = appPath.ToString();
            }
            return startPath;
        }

        /// <summary>
        /// 获取Request.QueryString的值,去空格
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetQ(this System.Web.HttpRequest Request, string key) {
            string v = Request.QueryString[key];
            if (string.IsNullOrEmpty(v)) return "";
            else return v.Trim();
        }
        /// <summary>
        /// 获取Request.Form的值,去空格
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetF(this System.Web.HttpRequest Request, string key) {
            string v = Request.Form[key];
            if (string.IsNullOrEmpty(v)) return "";
            else return v.Trim();
        }
    }
}
